#include<stdio.h>
#include<unistd.h>
#include<assert.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>


int main(int argc, char* argv[])
{
    printf("process is running...\n");
    pid_t id = fork();
    assert(id != -1);

    if(id == 0)
    {
        //这里的替换会影响父进程吗？不会，进程具有独立性，存在写时拷贝
        //child
        //格式类比：命令行怎么写，这里就怎么传
        sleep(1);
        // ./exec ls -a -l -> "./exec" "ls" "-a" "-l"
        execvp(argv[1], &argv[1]);
        //execl("usr/bin/ls", "ls", "-a", "-l", "--color=auto", NULL);
        //execlp("ls", "ls", "-a", "-l", "--color=auto", NULL);//只写一个ls也可以
       // char *const argv_[] = {
       //     "ls",
       //     "-a",
       //     "-l",
       //     "--color=auto",
       //     NULL 
       // };
       // execvp("ls", argv_);
       // //execv("/usr/bin/ls", argv_);
       //execl("./mycpp", "mycpp", NULL);
        //char* const envp_[] = {
        //    (char*)"MYENV=1111222233334444",
        //    NULL 
        //};
        //extern char** environ;
        //putenv((char*)"MYENV=44332211");//将指定环境变量导入到系统中 environ指向的环境变量表中
        ////execle("./mybin", "mybin", NULL, envp_);//自定义环境变量
        //execle("./mybin", "mybin", NULL, environ);
        exit(-1); // must failed
    }

    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0)  printf("wait success: exit code: %d, sig: %d\n", (status>>8)&0xFF, status & 0x7F);
    return 0;










 //   //.c->exe->load->process->运行->执行我们现在所写的代码
 //   printf("process is running...\n");
 //   // load->exe
 //   execl("/usr/bin/ls", "ls", "--color=auto", "-a", "-l", NULL/*你想怎么执行*/);//all exec* end of NULL 
 //   printf("process is running...\n");
 //   return 0;
}
